package org.shopen.puremvc.model.utils
{
	import flash.events.EventDispatcher;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.mxml.HTTPService;

	public class TextLoader extends EventDispatcher
	{
		[Bindable]
		private var _id : String = "";
		[Bindable]
		public function get id() : String
		{
			return _id;
		}
		public function set id(value : String) : void
		{
			_id = value;
		}
		
		[Bindable]
		private var _result : String = "";
		[Bindable]
		public function get result() : String
		{
			return _result;
		}
		public function set result(value : String) : void
		{
			_result = value;
		}
		
		[Bindable]
		private var _url : String = "";
		[Bindable]
		public function get url() : String
		{
			return _url;
		}
		public function set url(value : String) : void
		{
			_url = value;
		}
		
		[Bindable]
		private var _vo : Object = "";
		[Bindable]
		public function get vo() : Object
		{
			return _vo;
		}
		public function set vo(value : Object) : void
		{
			_vo= value;
		}
		
		
		private var _service : HTTPService;
		protected function get service() : HTTPService
		{
			return _service;
		}
		protected function set service(value : HTTPService) : void
		{
			_service = value;
		}
		
		public function TextLoader(id : String, url:String, object : Object)
		{
			this.id = id;
			this.url = url;
			this.vo = object;
			setService();
		}
		
		protected function setService() : void
		{
			service = new HTTPService();
			service.url = url;
			service.resultFormat = "text";
			service.addEventListener(ResultEvent.RESULT, serviceResultListener);
			service.addEventListener(FaultEvent.FAULT, serviceFaultListener);
		}
		
		public function load() : void
		{
			service.send();
		}
		
		private function serviceResultListener(event : ResultEvent) : void
		{
			var result : String = String(event.result);
			this.result = result;
			dispatchEvent(ResultEvent.createEvent(result));
		}
		
		private function serviceFaultListener(event : FaultEvent) : void
		{			
			dispatchEvent(FaultEvent.createEvent(event.fault));
		}
	}
}